package com.yanqu.road.dao.impl.activity.unionchest;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.unionChest.CrossUnionChestNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionChestNoticeDaoImpl extends TempDao {
    public static String getCrossUnionChestNoticeDataSql = "select * from `t_c_unionchest_notice` where `activityId`=?";

    public List<CrossUnionChestNoticeData> getCrossUnionChestNoticeDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossUnionChestNoticeData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossUnionChestNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionChestNoticeData noticeData = parseNoticeData(rs);
                dataList.add(noticeData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String insertCrossUnionChestNoticeDataSql = "insert into `t_c_unionchest_notice`(activityId,unionId,boxId,boxType,shareName,opendUserId,opendName,opendTime,rewards) values (?,?,?,?,?,?,?,?,?)";

    public void addNoticeListData(List<CrossUnionChestNoticeData> dataList) {
        for (CrossUnionChestNoticeData crossUnionChestNoticeData : dataList) {
            crossUnionChestNoticeData.setBeginWriteTime();
            getDb().setConnName(DbNameStrategy.getCross2Db());
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestNoticeData.getActivityId()));
            para.put(i++, new DbParameter(Types.VARCHAR, crossUnionChestNoticeData.getUnionId()));
            para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestNoticeData.getBoxId()));
            para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestNoticeData.getBoxType()));
            para.put(i++, new DbParameter(Types.VARCHAR, crossUnionChestNoticeData.getShareName()));
            para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestNoticeData.getOpendUserId()));
            para.put(i++, new DbParameter(Types.VARCHAR, crossUnionChestNoticeData.getOpendName()));
            para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestNoticeData.getOpendTime()));
            para.put(i++, new DbParameter(Types.VARCHAR, crossUnionChestNoticeData.getRewards()));
            getDb().executeNoneQuery(insertCrossUnionChestNoticeDataSql, para);
        }
    }

    protected CrossUnionChestNoticeData parseNoticeData(ResultSet rs) throws SQLException {
        CrossUnionChestNoticeData crossUnionChestNoticeData = new CrossUnionChestNoticeData();
        crossUnionChestNoticeData.setActivityId(rs.getInt("activityId"));
        crossUnionChestNoticeData.setUnionId(rs.getString("unionId"));
        crossUnionChestNoticeData.setBoxId(rs.getLong("boxId"));
        crossUnionChestNoticeData.setBoxType(rs.getInt("boxType"));
        crossUnionChestNoticeData.setShareName(rs.getString("shareName"));
        crossUnionChestNoticeData.setOpendUserId(rs.getLong("opendUserId"));
        crossUnionChestNoticeData.setOpendName(rs.getString("opendName"));
        crossUnionChestNoticeData.setOpendTime(rs.getLong("opendTime"));
        crossUnionChestNoticeData.setRewards(rs.getString("rewards"));

        return crossUnionChestNoticeData;
    }
}
